<?php

//decode by 012wz.com QQ:800083075
global $_W, $_GPC;
$type = intval($_GPC['type']);
$operation = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($operation == 'display') {
	ca('creditshop.log.view' . $type);
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$condition = ' and log.uniacid=:uniacid and g.type=:type and log.status>0';
	$params = array(':uniacid' => $_W['uniacid'], ':type' => $type);
	if (!empty($_GPC['keyword'])) {
		$_GPC['keyword'] = trim($_GPC['keyword']);
		$condition .= ' and ( log.logno like :keyword or log.eno like :keyword or g.title like :keyword ) ';
		$params[':keyword'] = "%{$_GPC['keyword']}%";
	}
	if ($_GPC['status'] != '') {
		$condition .= " and log.status=" . intval($_GPC['status']);
	}
	if (!empty($_GPC['realname'])) {
		$_GPC['realname'] = trim($_GPC['realname']);
		$condition .= ' and ( m.realname like :realname or m.nickname like :realname or m.mobile like :realname ) ';
		$params[':realname'] = "%{$_GPC['realname']}%";
	}
	if (empty($starttime) || empty($endtime)) {
		$starttime = strtotime('-1 month');
		$endtime = time();
	}
	$searchtime = $_GPC['searchtime'];
	if (!empty($_GPC['searchtime'])) {
		$starttime = strtotime($_GPC['time']['start']);
		$endtime = strtotime($_GPC['time']['end']);
		if (!empty($searchtime)) {
			$condition .= " AND log.createtime >= :starttime AND log.createtime <= :endtime ";
			$params[':starttime'] = $starttime;
			$params[':endtime'] = $endtime;
		}
	}
	$list = pdo_fetchall("select *,log.status,log.createtime,log.credit as credit2,g.type as goodstype,log.id as logid,log.gold_card,log.weixin from " . tablename('ewei_shop_goldshop_log') . " log " . " left join " . tablename('ewei_shop_member') . " m on m.openid = log.openid and m.uniacid=5" ." left join " . tablename('ewei_shop_goldshop_goods') . " g on g.id = log.goodsid" ." where 1 {$condition}  order by log.createtime desc limit " . ($pindex - 1) * $psize . ',' . $psize,$params);
	$total = pdo_fetchcolumn("select count(log.id) from" . tablename('ewei_shop_goldshop_log') . " log " . " left join " . tablename('ewei_shop_member') . " m on m.openid = log.openid and m.uniacid=:uniacid" . " left join " . tablename('ewei_shop_member_address') . " a on a.id = log.addressid" . " left join " . tablename('ewei_shop_goldshop_goods') . " g on g.id = log.goodsid" . " where 1 {$condition}", $params);
        foreach ($list as &$row) {
		$row['address'] = array();
		$row['address'] = pdo_fetch('select realname,mobile,address,province,city,area from ' . tablename('ewei_shop_goldshop_log') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $row['logid'], ':uniacid' => $_W['uniacid']));
		$row['address']['logid'] = $row['logid'];
		$canexchange =true;
		if ($row['status'] == 2) {
			if (empty($row['paystatus'])) {
				$canexchange = false;
			}
			if (empty($row['dispatchstatus'])) {
				$canexchange = false;
			}
		} else {
			$canexchange = false;
		}
		$row['canexchange'] = $canexchange;
	}
        if($_GPC['submit_modal'] == "导出模板"){
        ca('order.op.export');
	plog('order.op.export', '导出兑换记录');
        $columns =array(array('title' => '活动编号', 'field' => 'ordersn', 'width' => 24),array('title' => '快递公司', 'field' => 'expresscom', 'width' => 24),array('title' => '快递单号', 'field' => 'expresssn', 'width' => 24));
        m('excel')->export(" ", array("title" => "待确认兑换模板-" . date('Y-m-d-H-i', time()), "columns" => $columns));
        }
        if($_GPC['submit_expro'] == "批量兑换"){
        ca('creditshop.log.exchange');
        $info=m('excel')->import("import_expro");
        foreach($info as $key=>$value){
        $value[0]=trim($value[0]);$value[1]=trim($value[1]);$value[2]=trim($value[2]);
        if($value[1]=="顺丰"){$express="shunfeng";}if($value[1]=="申通"){$express="shentong";}if($value[1]=="韵达快运"){$express="yunda";}if($value[1]=="天天快递"){$express="tiantian";}if($value[1]=="圆通速递"){$express="yuantong";}
        if($value[1]=="中通速递"){$express="zhongtong";}if($value[1]=="ems快递"){$express="ems";}if($value[1]=="汇通快运"){$express="huitongkuaidi";}if($value[1]=="全峰快递"){$express="quanfengkuaidi";}if($value[1]=="宅急送"){$express="zhaijisong";}
        if($value[1]=="aae全球专递"){$express="aae";}if($value[1]=="安捷快递"){$express="anjie";}if($value[1]=="安信达快递"){$express="anxindakuaixi";}if($value[1]=="彪记快递"){$express="biaojikuaidi";}if($value[1]=="bht"){$express="bht";}
        if($value[1]=="百福东方国际物流"){$express="baifudongfang";}if($value[1]=="中国东方（COE）"){$express="coe";}if($value[1]=="长宇物流"){$express="changyuwuliu";}if($value[1]=="大田物流"){$express="datianwuliu";}if($value[1]=="德邦物流"){$express="debangwuliu";}
        if($value[1]=="dhl"){$express="dhl";}if($value[1]=="dpex"){$express="dpex";}if($value[1]=="d速快递"){$express="dsukuaidi";}if($value[1]=="递四方"){$express="disifang";}if($value[1]=="fedex（国外）"){$express="fedex";}
        if($value[1]=="飞康达物流"){$express="feikangda";}if($value[1]=="凤凰快递"){$express="fenghuangkuaidi";}if($value[1]=="飞快达"){$express="feikuaida";}if($value[1]=="国通快递"){$express="guotongkuaidi";}if($value[1]=="港中能达物流"){$express="ganzhongnengda";}  
        if($value[1]=="广东邮政物流"){$express="guangdongyouzhengwuliu";}if($value[1]=="共速达"){$express="gongsuda";}if($value[1]=="恒路物流"){$express="hengluwuliu";}if($value[1]=="华夏龙物流"){$express="huaxialongwuliu";}if($value[1]=="海红"){$express="haihongwangsong";}
        if($value[1]=="海外环球"){$express="haiwaihuanqiu";}if($value[1]=="佳怡物流"){$express="jiayiwuliu";}if($value[1]=="京广速递"){$express="jinguangsudikuaijian";}if($value[1]=="急先达"){$express="jixianda";}if($value[1]=="佳吉物流"){$express="jjwl";}
        if($value[1]=="加运美物流"){$express="jymwl";}if($value[1]=="金大物流"){$express="jindawuliu";}if($value[1]=="嘉里大通"){$express="jialidatong";}if($value[1]=="晋越快递"){$express="jykd";}if($value[1]=="快捷速递"){$express="kuaijiesudi";}
        if($value[1]=="联邦快递（国内）"){$express="lianb";}if($value[1]=="联昊通物流"){$express="lianhaowuliu";}if($value[1]=="龙邦物流"){$express="longbanwuliu";}if($value[1]=="立即送"){$express="lijisong";}if($value[1]=="乐捷递"){$express="lejiedi";}
        if($value[1]=="民航快递"){$express="minghangkuaidi";}if($value[1]=="美国快递"){$express="meiguokuaidi";}if($value[1]=="门对门"){$express="menduimen";}if($value[1]=="OCS"){$express="ocs";}if($value[1]=="配思货运"){$express="peisihuoyunkuaidi";}
        if($value[1]=="全晨快递"){$express="quanchenkuaidi";}if($value[1]=="全际通物流"){$express="quanjitong";}if($value[1]=="全日通快递"){$express="quanritongkuaidi";}if($value[1]=="全一快递"){$express="quanyikuaidi";}if($value[1]=="如风达"){$express="rufengda";}
        if($value[1]=="三态速递"){$express="santaisudi";}if($value[1]=="盛辉物流"){$express="shenghuiwuliu";}if($value[1]=="速尔物流"){$express="sue";}if($value[1]=="盛丰物流"){$express="shengfeng";}if($value[1]=="赛澳递"){$express="saiaodi";}
        if($value[1]=="天地华宇"){$express="tiandihuayu";}if($value[1]=="tnt"){$express="tnt";}if($value[1]=="ups"){$express="ups";}if($value[1]=="万家物流"){$express="wanjiawuliu";}if($value[1]=="文捷航空速递"){$express="wenjiesudi";}
        if($value[1]=="伍圆"){$express="wuyuan";}if($value[1]=="万象物流"){$express="wxwl";}if($value[1]=="新邦物流"){$express="xinbangwuliu";}if($value[1]=="信丰物流"){$express="xinfengwuliu";}if($value[1]=="亚风速递"){$express="yafengsudi";}
        if($value[1]=="一邦速递"){$express="yibangwuliu";}if($value[1]=="优速物流"){$express="youshuwuliu";}if($value[1]=="邮政包裹挂号信"){$express="youzhengguonei";}if($value[1]=="邮政国际包裹挂号信"){$express="youzhengguoji";}
        if($value[1]=="远成物流"){$express="yuanchengwuliu";}if($value[1]=="源伟丰快递"){$express="yuanweifeng";}if($value[1]=="元智捷诚快递"){$express="yuanzhijiecheng";}if($value[1]=="运通快递"){$express="yuntongkuaidi";}if($value[1]=="越丰物流"){$express="yuefengwuliu";}
        if($value[1]=="源安达"){$express="yad";}if($value[1]=="银捷速递"){$express="yinjiesudi";}if($value[1]=="中铁快运"){$express="zhongtiekuaiyun";}if($value[1]=="中邮物流"){$express="zhongyouwuliu";} if($value[1]=="忠信达"){$express="zhongxinda";}if($value[1]=="芝麻开门"){$express="zhimakaimen";}
       	$log = pdo_fetch('select * from ' . tablename('ewei_shop_goldshop_log') . ' where logno=:logno and uniacid=:uniacid limit 1', array(':logno' => $value[0], ':uniacid' => $_W['uniacid']));
        $member = m('member')->getMember($log['openid'], true);
	$goods = $this->model->getGoods($log['goodsid'], $member);
            if(!empty($value[0]) && !empty($value[1]) && !empty($value[2]) && !empty($log) && !empty($goods['id']) && $log["status"]==2 && !empty($log['paystatus']) && $goods['dispatch'] <= 0 && !empty($log['dispatchstatus'])){
            pdo_update('ewei_shop_goldshop_log',array('status'=>3,"expresscom"=>$value[1],"expresssn"=>$value[2],"usetime"=>time(),'express' => $express,'sendtime'=>time()), array("logno"=>$value[0], 'uniacid' => $_W['uniacid'])); 
            $this->model->sendMess($log['id']);
	    plog('creditshop.log.exchange', "金币商城兑换 兑换记录ID: {$log['id']}");
           }
        }
	message('批量兑换成功！', $this->createPluginWebUrl('goldshop/log'), 'success');
        }
        if ($_GPC['export'] == 1) {
	$listall = pdo_fetchall("select *,log.status,log.realname as realnamelog,log.mobile as mobilelog,log.province as provincelog,log.city as citylog,log.area as arealog,log.address as addresslog,log.createtime,g.type as goodstype,log.id as logid,log.gold_card,log.credit as credit2,log.weixin from " . tablename('ewei_shop_goldshop_log') . " log " . " left join " . tablename('ewei_shop_member') . " m on m.openid = log.openid and m.uniacid=5" ." left join " . tablename('ewei_shop_goldshop_goods') . " g on g.id = log.goodsid" ." where log.uniacid=:uniacid and log.status>0 and g.type= {$type} order by log.createtime desc  " ,array(':uniacid' => $_W['uniacid']));
        foreach ($listall as &$row) {
		$row['address'] = array();
		if (!empty($row['addressid'])) {
			$row['address'] = pdo_fetch('select realname,mobile,address,province,city,area from ' . tablename('ewei_shop_member_address') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $row['addressid'], ':uniacid' => $_W['uniacid']));
		}
		$row['address']['logid'] = $row['id'];
		$canexchange = true;
		
		if ($row['status'] == 2) {
			if (empty($row['paystatus'])) {
				$canexchange = false;
			}
			if (empty($row['dispatchstatus'])) {
				$canexchange = false;
			}
		} else {
			$canexchange = false;
		}
		$row['canexchange'] = $canexchange;
	}
		ca('order.op.export');
		plog('order.op.export', '导出兑换记录');
		$columns = array(array('title'=>'兑换ID','field'=>'logid','width'=>12),array('title' => '活动编号', 'field' => 'logno', 'width' => 24), array('title' => '商品名称', 'field' => 'title', 'width' => 12),array('title' => '收件人', 'field' => 'realname', 'width' => 12),array('title' => '联系电话', 'field' => 'mobile', 'width' => 12),array('title' => '收货地址', 'field' => 'address', 'width' => 24), array('title' => '兑换时间', 'field' => 'createtime', 'width' => 12), array('title' => '兑换码', 'field' => 'eno', 'width' => 12), array('title' => '类型', 'field' => 'goodstype', 'width' => 12), array('title' => '用户信息', 'field' => 'nickname', 'width' => 12), array('title' => '消耗', 'field' => 'credit', 'width' => 12), array('title' => '状态', 'field' => 'status', 'width' => 12), array('title' => '支付状态', 'field' => 'paytype', 'width' => 12), array('title' => '运费状态', 'field' => 'dispatchstatus', 'width' => 12));
		$exportlist = array();
		foreach ($listall as &$row){
                 if($row['canexchange'] && $row['status']){
                  $r['logid']=$row['logid'];
                  $r['logno']=$row['logno'];
                  $r['title']=$row['title'];
                  $r['realname']=$row['realnamelog'];
                  $r['mobile']=$row['mobilelog'];
                  $r['address']=$row['provincelog']." ".$row['citylog']." ".$row['arealog']." ".$row['addresslog'];
                  $r['createtime']=date('Y-m-d H:i:s', $row['createtime']);
                  $r['eno']=$row['eno'];
                  $r['goodstype']="兑换";
                  $r['nickname']=$row['nickname'];
                  if($row['gold_card']>0 && $row['credit2']>0){
                  $r['credit']='-'.$row['gold_card']."金币".'，-'.$row['credit2']."现金";    
                  }
                  if($row['gold_card']>0 && $row['weixin']>0){
                  $r['credit']='-'.$row['gold_card']."金币".'，-'.$row['weixin']."现金";    
                  }
                  if($row['gold_card']==0 && $row['credit2']>0){
                  $r['credit']='-'.$row['credit2']."现金";    
                  }
                  if($row['gold_card']==0 && $row['weixin']>0){
                  $r['credit']='-'.$row['weixin']."现金";    
                  }
                  $r['status']="待发货";
                  if($row['paytype']==-1){
                  $r['paytype']="无需支付"; 
                  }elseif($row['paytype']==0){
                        if($row['paystatus']==0){
                        $r['paytype']="余额未支付";
                        }else{
                        $r['paytype']="余额已支付";    
                        }   
                  }elseif($row['paytype']==1){
                       if($row['paystatus']==0){
                        $r['paytype']="微信未支付";   
                       }else{
                        $r['paytype']="微信已支付";   
                       }  
                  }
                  if($row['dispatchstatus']==-1){
                  $r['dispatchstatus']="无需支付";  
                  }elseif($row['dispatchstatus']==0){
                  $r['dispatchstatus']="未支付";    
                  }elseif($row['dispatchstatus']==1){
                  $r['dispatchstatus']="已支付";    
                  }
		$exportlist[] = $r;
                 }
                 unset($r);
		}              
		m('excel')->export($exportlist, array("title" => "兑换记录数据-" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
	unset($row);
	$pager = pagination($total, $pindex, $psize);
} else {
	if ($operation == 'detail') {
                if($_GPC['submit']=="确认修改"){
                $data=array(
                'realname'=>$_GPC['realname'],
                'mobile'=>$_GPC['mobile'],
                'province'=>$_GPC['province'],
                'city'=>$_GPC['city'],
                'area'=>$_GPC['area'],
                'address'=>$_GPC['address'] 
                );
                pdo_update('ewei_shop_goldshop_log',$data,array('id' =>$_GPC['id']));
                message('修改收件人信息成功!',$this->createPluginWebUrl('goldshop/log',array('op'=>'detail','id' => $_GPC['id'])), 'success');
                }
		$id = intval($_GPC['id']);
		$log = pdo_fetch('select * from ' . tablename('ewei_shop_goldshop_log') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $id, ':uniacid' => $_W['uniacid']));
		if (empty($log)) {
			message('兑换记录不存在!', referer(), 'error');
		}
		$type = $log['type'];
		ca('creditshop.log.view' . $log['type']);
		$member = m('member')->getMember($log['openid'], true);
		$goods = $this->model->getGoods($log['goodsid'], $member);
		if (empty($goods['id'])) {
			message('商品记录不存在!', referer(), 'error');
		}
		$canexchange = true;
		if ($log['status'] == 2) {
			if (empty($log['paystatus'])) {
				$canexchange = false;
			}
			if (empty($log['dispatchstatus'])) {
				$canexchange = false;
			}
		} else {
			$canexchange = false;
		}
		$log['canexchange'] = $canexchange;
		$address = false;
		if (!empty($log['addressid'])) {
			$address = pdo_fetch('select id,realname,mobile,address,province,city,area from ' . tablename('ewei_shop_member_address') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $log['addressid'], ':uniacid' => $_W['uniacid']));
		}
		$address['logid'] = $id;
	} else {
		if ($operation == 'exchange') {
			ca('creditshop.log.exchange');
			$id = intval($_GPC['id']);
			$log = pdo_fetch('select * from ' . tablename('ewei_shop_goldshop_log') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $id, ':uniacid' => $_W['uniacid']));
                        if (empty($log)) {
				message('兑换记录不存在!', referer(), 'error');
			}
			if (empty($log['status'])) {
				message('无效兑换记录!', referer(), 'error');
			}
			if ($log['status'] >= 3) {
				message('此记录已兑换过了!', referer(), 'error');
			}
			$member = m('member')->getMember($log['openid'], true);
			$goods = $this->model->getGoods($log['goodsid'], $member);
			if (empty($goods['id'])) {
				message('商品记录不存在!', referer(), 'error');
			}
			if (!empty($goods['type'])) {
				if ($log['status'] <= 1) {
					message('未中奖，不能兑换!', referer(), 'error');
				}
			}
			if ($goods['money'] > 0 && empty($log['paystatus'])) {
				message('未支付，无法进行兑换!', referer(), 'error');
			}
			if ($goods['dispatch'] > 0 && empty($log['dispatchstatus'])) {
				message('未支付运费，无法进行兑换!', referer(), 'error');
			}
			pdo_update('ewei_shop_goldshop_log', array('status' => 3, 'usetime' => time(), 'expresscom' => $_GPC['expresscom'], 'expresssn' => $_GPC['expresssn'], 'express' => $_GPC['express'],'sendtime'=>time()), array('id' => $id));
			$this->model->sendMess($id);
			plog('creditshop.log.exchange', "金币商城兑换 兑换记录ID: {$id}");
			message('兑换成功!', $this->createPluginWebUrl('goldshop/log', array('type' => $goods['type'])), 'success');
		}
	}
}
load()->func('tpl');
include $this->template('log');